package com.capgemini.cn.authority.core.security;

import com.capgemini.cn.authority.core.authentication.CheckRoleAndPermissionAuthorizer;
import com.capgemini.cn.authority.core.utils.SpringProxyUtils;
import org.pac4j.core.authorization.authorizer.Authorizer;
import org.pac4j.core.client.Clients;
import org.pac4j.core.config.Config;
import org.pac4j.core.util.CommonHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * <p>权限配置类，用以配置所有权限基础信息</p>
 * Created by 2017/8/28
 *
 * @author yaxia
 */
public class SecurityConfig extends Config {

    @Autowired
    @Qualifier(value = SecurityClient.COMPONENT_NAME)
    public void setClient(SecurityClient client) {
        this.setClients(new Clients(client));
    }

    @Autowired
    @Qualifier(value = CheckRoleAndPermissionAuthorizer.COMPONENT_NAME)
    @Override
    public void setAuthorizer(Authorizer authorizer) {
        CommonHelper.assertNotNull("authorizer", authorizer);
        // 因为对象是被Spring动态代理的，所以这里需要手动获取被代理的原始对象信息
        this.authorizers.put(SpringProxyUtils.getTarget(authorizer).getSimpleName(), authorizer);
    }

    public void setPathMatcher(String pathMatcher) {
        this.setMatcher(new SecurityPathMatcher(pathMatcher));
    }

}
